﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Vds2465Telegrams;

namespace Vds2465Parser
{
    internal static class GenParse_ReportType_Technical
    {
        #region private constants

        internal const byte MsgBlockOnlyValue = 0x40;

        private const byte GeneralByte = 0x00;
        private const byte AlarmByte = 0x01;
        private const byte Emergency_GeneralByte = 0x08;
        private const byte Emergency_1Byte = 0x09;
        private const byte Emergency_2Byte = 0x0A;
        private const byte Emergency_3Byte = 0x0B;
        private const byte Emergency_4Byte = 0x0C;
        
        #endregion private constants

        #region internal methods

        internal static byte GenerateByte(ReportType_Technical reportTypeTechnical)
        {
            byte resultByte = 0x0;

            // Technical has 0x1 in Bits 6,5,4
            resultByte += MsgBlockOnlyValue;

            // add the report type sub type
            if (reportTypeTechnical.ReportSubType == eReportSubType_Technical.General)
            {
                resultByte |= GeneralByte;
                return resultByte;
            }

            if (reportTypeTechnical.ReportSubType == eReportSubType_Technical.Alarm)
            {
                resultByte |= AlarmByte;
                return resultByte;
            }

            if (reportTypeTechnical.ReportSubType == eReportSubType_Technical.Emergency_1)
            {
                resultByte |= Emergency_1Byte;
                return resultByte;
            }

            if (reportTypeTechnical.ReportSubType == eReportSubType_Technical.Emergency_2)
            {
                resultByte |= Emergency_2Byte;
                return resultByte;
            }

            if (reportTypeTechnical.ReportSubType == eReportSubType_Technical.Emergency_3)
            {
                resultByte |= Emergency_3Byte;
                return resultByte;
            }

            if (reportTypeTechnical.ReportSubType == eReportSubType_Technical.Emergency_4)
            {
                resultByte |= Emergency_4Byte;
                return resultByte;
            }

            if (reportTypeTechnical.ReportSubType == eReportSubType_Technical.Emergency_General)
            {
                resultByte |= Emergency_GeneralByte;
                return resultByte;
            }

            // TODO handle error (unknown eReportSubType_Technical)
            return resultByte;
        }

        internal static bool ParseByte(byte byteValue, out eReportSubType_Technical reportSubTypeTechnical)
        {
            // set arbitary default value
            reportSubTypeTechnical = eReportSubType_Technical.General;

            // get only bits 6,5,4
            byte messageBlock = (byte)(byteValue & (byte)0x70);
            if (messageBlock == MsgBlockOnlyValue)
            {
                // get only bits 3,2,1,0
                byte subType = (byte)(byteValue & (byte)0x0f);
                if (subType == GeneralByte)
                {
                    reportSubTypeTechnical = eReportSubType_Technical.General;
                    return true;
                }

                if (subType == AlarmByte)
                {
                    reportSubTypeTechnical = eReportSubType_Technical.Alarm;
                    return true;
                }

                if (subType == Emergency_GeneralByte)
                {
                    reportSubTypeTechnical = eReportSubType_Technical.Emergency_General;
                    return true;
                }

                if (subType == Emergency_1Byte)
                {
                    reportSubTypeTechnical = eReportSubType_Technical.Emergency_1;
                    return true;
                }

                if (subType == Emergency_2Byte)
                {
                    reportSubTypeTechnical = eReportSubType_Technical.Emergency_2;
                    return true;
                }

                if (subType == Emergency_3Byte)
                {
                    reportSubTypeTechnical = eReportSubType_Technical.Emergency_3;
                    return true;
                }

                if (subType == Emergency_4Byte)
                {
                    reportSubTypeTechnical = eReportSubType_Technical.Emergency_4;
                    return true;
                }              

                // bits 3,2,1,0 are wrong
                return false;
            }

            // bits 6,5,4 are wrong
            return false;
        }

        #endregion internal methods
    }
}
